package com.drivemanage.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.drivemanage.dao.OrderDao;
import com.drivemanage.entry.DriveResult;
import com.drivemanage.entry.OrderInfo;
import com.drivemanage.utils.OpenUtils;

/**
 * @version 创建时间：2017-5-19 上午11:13:42
 *          程序的简单说明
 */
@Service
// 追加事务管理
@Transactional
public class OrderInfoServiceImpl implements OrderInfoService {

    @Resource
    private OrderDao orderdao;

    /**
     * 新增订单
     */
    @Override
    public DriveResult newOrder(String carid, String userid, String seluser, String selcar, String linkphone,
            String address, String starttime, String endtime) {
        String orderid = OpenUtils.createOrderID();
        String opentime = OpenUtils.getSysDate();
        OrderInfo orderInfo = new OrderInfo(orderid, carid, selcar, userid, seluser, linkphone, address, starttime,
                endtime, "0", opentime, "", "", "");
        orderdao.neworder(orderInfo);
        return new DriveResult(DriveResult.SUCCESS_CODE, "预约成功，可在个人中心查看订单状态", orderid);
    }

    @Override
    public DriveResult getMyOrder(String checkTag) {
        if ("全部订单".equals(checkTag)) {
            checkTag = "0";
        }
        if ("进行中".equals(checkTag)) {
            checkTag = "1";
        }
        if ("已完成".equals(checkTag)) {
            checkTag = "2";
        }
        List<OrderInfo> myOrders = orderdao.qryMyOrder(checkTag);
        if (OpenUtils.isEmpty(myOrders)) {
            return new DriveResult(DriveResult.ERROR_CODE, "未查询到订单数据", "");
        }
        return new DriveResult(DriveResult.SUCCESS_CODE, "查询成功", myOrders);
    }

    /**
     * 修改订单状态
     */
    @Override
    public DriveResult modifyOrderStatus(int orderstatus, String difdo, String orderid) {
        String status = "";
        if (1 == orderstatus) {// 试驾-还车
            status = "2";
            Map param = new HashMap();
            param.put("status", status);
            param.put("orderid", orderid);
            orderdao.setstatus(param);
            return new DriveResult(DriveResult.SUCCESS_CODE, "还车成功", "");
        }
        return new DriveResult(DriveResult.ERROR_CODE, "异常错误", "");
    }

}
